package jianzhiOffer;

public class num29 {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        int l = 0,t = 0;
        int r = matrix[0].length - 1,b = matrix.length - 1;
        int[] arr = new int[(r + 1) * (b + 1)];
        int x = 0;
        while(true) {
            for(int i = l; i <= r; i++) arr[x++] = matrix[t][i];
            if(++t > b) break;
            for(int i = t; i <= b; i++) arr[x++] = matrix[i][r];
            if(--r < l) break;
            for(int i = r; i >= l; i--) arr[x++] = matrix[b][i];
            if(--b < t) break;
            for(int i = b; i >= t; i--) arr[x++] = matrix[i][l];
            if(++l > r) break;
        }
        return arr;
    }
}
